
<?php 

include 'db_params.php';
### Inicializa Sesion UC
include_once('CAS-UC/CAS.php');

// Uncomment to enable debugging
phpCAS::setDebug();

// Inicializa librería phpCAS
phpCAS::client(CAS_VERSION_2_0, 'sso.uc.cl', 443, '/cas');
phpCAS::setNoCasServerValidation();
//exije estar Logueado uc
phpCAS::forceAuthentication();


#Una vez logueado guarda una variable de sesion boolean que indica si está logueado o no 
if (!isset($_SESSION['username'])) {
   $_SESSION['username'] = phpCAS::getUser();//this stores their network user id
}
else
    {
    phpCAS::forceAuthentication();
    }
$username=$_SESSION['username'];

    ###Para hacer logout.
    if (isset($_REQUEST['logout'])) {
   $_SESSION['loggedInLocally'] = false;
   unset($_SESSION['username']);
phpCAS::logout();   
}
?>

<?php

//CREDENCIALES DE ACCESO

// Include FB SDK
require 'facebook-php-sdk/src/facebook.php';

// Create our Application instance
$facebook = new Facebook(array(
  'appId'  => '171539856240090',
  'secret' => '68c7553a8d08f8d85839a76f427f2092',
  'cookie' => true,
));

#####



// Get session for checking
//$session = $facebook->getSession();
  //Try to request FB variables
  try {
      
      $query = mysql_query("SELECT uid_facebook FROM usuarios WHERE user_name='$username'");
	$r=mysql_num_rows($query);
	//echo $query;
	//If not tuples fetched need to register
	if($r==0){
		$query=725338756;
		}
		//Session is created to speedup 
    $query=mysql_fetch_array($query);
    $query=$query['uid_facebook'];            
    $uid = $query;
    //$me = $facebook->api('/'.$uid);
	} 
        catch (FacebookApiException $e) {
	//If error on FB vars fetch -> need to login
	header('Location: http://www.turnos.feuc.cl/index.php?action=loginerror');
	}


?>





<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>Turnos UC</title>

		<link href="css/styles.css" rel="stylesheet" type="text/css" /><!-- General Styles -->
		<link rel="stylesheet" type="text/css" href="js/fancybox/jquery.fancybox-1.3.4.css" media="screen" /><!-- Fancybox -->
		<!--<link href="css/home-styles.css" rel="stylesheet" type="text/css" />--> <!-- Home Specific Styles -->
		<link href="css/dashboard.css" rel="stylesheet" type="text/css" 
                      
                      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
                <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
                
                <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.css" type="text/css" />
                <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" type="text/css" />


		<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
		<script src="js/utf8_decode.js"></script>


		<script> var fuid=<?php echo $uid;?>;</script>
		
		<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAdvXAW3x9mWzFu3My7JB1EBRl7lPwFGHthOmeAAsZ97l6ruTJ8xTmhLGTimZefYmJjsPqapDjgjdtIg"
            type="text/javascript"></script>

    <script type="text/javascript">
    //<![CDATA[

	var iconBlue = new GIcon(); 
    iconBlue.image = 'https://graph.facebook.com/100000059368121/picture';
    iconBlue.shadow = 'http://www.turnos.feuc.cl/shadow.png';
    iconBlue.iconSize = new GSize(50, 50);
    iconBlue.shadowSize = new GSize(75, 50);
    iconBlue.iconAnchor = new GPoint(6, 20);
    iconBlue.infoWindowAnchor = new GPoint(5, 1);

    var iconRed = new GIcon(); 
    iconRed.image = 'chinche.gif';
    iconRed.shadow = 'http://www.turnos.feuc.cl/shadow.png';
    iconBlue.iconSize = new GSize(50, 50);
    iconBlue.shadowSize = new GSize(75, 50);
    iconRed.iconAnchor = new GPoint(6, 20);
    iconRed.infoWindowAnchor = new GPoint(5, 1);

    var customIcons = [];
    customIcons["1"] = iconBlue;
    customIcons["0"] = iconRed;
	

    function load() {
      
        var map = new GMap2(document.getElementById("map"));
        var customUI = map.getDefaultUI();
        map.setUI(customUI);
        map.setCenter(new GLatLng(<?php
													$res = @mysql_query("select lat from direccion_usuarios where user_name='$username'");
													$row = mysql_fetch_array($res);
                                                    echo $row['lat'];
													
													?>, <?php
													$res = @mysql_query("select lng from direccion_usuarios where user_name='$username'");
													$row = mysql_fetch_array($res);
                                                    echo $row['lng'];?>
													), 14);

        GDownloadUrl("xmlgen.php?radio= <?php echo $_GET['radio'] ?> ", function(data) {
          var xml = GXml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("location");
          for (var i = 0; i < markers.length; i++) {
            var uid_facebook = markers[i].getAttribute("uid_facebook");
			var name = markers[i].getAttribute("name");
			var surename = markers[i].getAttribute("surename");
            var address = markers[i].getAttribute("address");
            var type = markers[i].getAttribute("type");
			var fb = markers[i].getAttribute("tiene_fb");
            var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));
            iconBlue.image = 'https://graph.facebook.com/'+uid_facebook+'/picture';
      		iconBlue.infoWindowAnchor = new GPoint(5, 1);
            var marker = createMarker(point, name, surename, uid_facebook, address, type, fb);
            map.addOverlay(marker);
			
          }
        });
      
    }

    function createMarker(point, name, surename, uid_facebook, address, type, fb) {
      var marker = new GMarker(point, customIcons[fb]);
      var html = '<TABLE> <tr><th><img src="auto'+type+'.png" width = "40px">' + "</th><th>" + name + " " + surename + "</br>" + address + "</th></tr></TABLE> ";
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }

    //]]>
  </script>
         <script>  
                $(function() {
		$( "#tabs" ).tabs({
			ajaxOptions: {
				error: function( xhr, status, index, anchor ) {
					$( anchor.hash ).html(
						"Couldn't load this tab. We'll try to fix this as soon as possible. " +
						"If this wouldn't be a demo." );
				}
			}
		});
	});
	</script>


  </head>

  <body  onLoad="load()" onUnload="GUnload()">

	
		<div id="fb-root"></div> <!-- Facebook requiered div!! -->

			<script> <!-- Facebook Init scripts -->
				  window.fbAsyncInit = function() {
					FB.init({
					  appId   : '171539856240090',
					  status  : false, // check login status ###
					  cookie  : true, // enable cookies to allow the server to access the session
					  xfbml   : true // parse XFBML
					});

					// whenever the user logs in, we refresh the page
					FB.Event.subscribe('auth.login', function() {
					  window.location="dashboard.php";
					});
				  };

				  (function() {
					var e = document.createElement('script');
					e.src = document.location.protocol + '//connect.facebook.net/es_ES/all.js';
					e.async = true;
					document.getElementById('fb-root').appendChild(e);
				  }());
			</script>
	
		
		<div class="container">

			<div class="sidebar1">
				<div id="home_right"> 
					<h3>Bienvenido</h3>
					<p><?php //echo $me['name']; ?></p>
										<?php
										$query = mysql_query("SELECT nombre, apellido1 FROM usuarios WHERE usuarios.user_name = '$username'");									
										$query = mysql_fetch_array($query);
										$nombre = $query['nombre'];
										$apellido1 = $query['apellido1'];
										?>
                                        <p><b><?php echo $nombre.' '.$apellido1;?></b></p>
										<p><a href="data_update.php">Editar mi información</a></p>
					<p><img src="https://graph.facebook.com/<?php echo $uid; ?>/picture" /></p>
				</div>
			</div><!-- end .sidebar1 -->
	  
			<div class="content">
	  
      
				<div id="main-content">


					<p>&nbsp;</p>
					<div class="col-left">
						<h2 class="heading" >Turnos UC</h2>
						<span class="sub-heading"></span>
					</div>
					<span class="thanks">
					
                    <div id="map" style="width: 700px; height: 400px">
                    
							</div>	
                            
					</span>
                    
                    
                    
                    
                    <form id="formulario" name="formulario"  method="GET" action="dashboard.php">
	

						<input type="text" name="radio" id="radio"/>
                        
                        
						<input type="submit" id="refrescar"/>
					</form>
                    
                    
                    
                    
                    

				</div>
                
                <p style="margin-top:20px; margin-bottom:40px"><fb:like href="turnos.feuc.cl" send="true" show_faces="true" font="" width="450"></fb:like></p>
                
			</div>
			
                        <div id="tabs">
	<ul>
		<li><a href="data_update.php">DU</a></li>
		<li><a href="registration.php">Reg</a></li>
                
		
	</ul>
                        
			<div class="footer">
				<p>Turnos UC - Ingenieria UC</p>
				<!-- end .footer -->
			</div>
		
		<!-- end .container -->
		</div>
	</body>

</html>